﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.Framework.Common.Enums;

using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.CharmManager.Client.Manager;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class TodoListManageView : BaseUserControl
    {
        string FieldName_STATETEXT = "TSTATETEXT";

        private DataSet dsTodoList;
        public TodoListManageView()
        {
            InitializeComponent();
            InitStatusComboBox();
        }

        public TodoListManageView(ClientAppBase app)
            : base(app)
        {
            InitializeComponent();

            ButtonItemIconHelper.InitRibbonBar(this.rbMenu);

            InitTodoList();
            InitStatusComboBox();
        }

        private void InitStatusComboBox()
        {
            List<string> list = new List<string>();
            list.Add(DBEntityToDoList.ENUM_TODOSTATE.T0N_CHINESE);
            list.Add(DBEntityToDoList.ENUM_TODOSTATE.T0I_CHINESE);
            list.Add(DBEntityToDoList.ENUM_TODOSTATE.T0T_CHINESE);
            list.Add(DBEntityToDoList.ENUM_TODOSTATE.T0V_CHINESE);

            cbiStatus.Items.Add("所有");
            foreach (string s in list)
            {
                cbiStatus.Items.Add(s);
            }

            cbiStatus.SelectedIndex = 2;
            //cbiStatus_SelectedIndexChanged(null, null);
        }

        private void btnModify_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvToDoList.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要修改的待办事项。");
                return;
            }

            DataRow dr = gvToDoList.GetDataRow(iRowHandle);
            DBEntityToDoList todoListInfo = new DBEntityToDoList(dr);
            frmToDoListInfo form = new frmToDoListInfo(null, todoListInfo);
            form.Mode = ControlClassModeEnum.emEdit;
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitTodoList();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvToDoList.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要删除的代办事项。");
                return;
            }
            bool bFlag = MsgBox.MsgQue("确定要删除此代办事项吗？");
            if (bFlag == true)
            {
                DataRow dr = gvToDoList.GetDataRow(iRowHandle);
                int todoId = Convert.ToInt32(dr[DBEntityToDoList.FieldName.TODOLISTID]);
                string error = string.Empty;
                bFlag = BussinessProxyManager.Instance.DeleteToDoList(todoId, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("删除代办事项失败\r\n" + error);
                    return;
                }
                else
                {
                    InitTodoList();
                }
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            frmToDoListInfo form = new frmToDoListInfo(null);
            if (form.ShowDialog() == DialogResult.OK)
            {
                InitTodoList();
            }
        }

        private void InitTodoList()
        {
            string sFilter = "";
            string error = string.Empty;
            dsTodoList = new DataSet();
            bool bFlag = BussinessProxyManager.Instance.QueryTodoList(sFilter, out dsTodoList, out error);
            if (bFlag == false)
            {
                MsgBox.MsgInfo("获取待办事项失败\r\n" + error);
                return;
            }

            
            DataColumn dc = new DataColumn(FieldName_STATETEXT);
            dsTodoList.Tables[0].Columns.Add(dc);
            foreach (DataRow dr in dsTodoList.Tables[0].Rows)
            {
                string sState = dr[DBEntityToDoList.FieldName.TODOSTATE].ToString();
                switch(sState)
                {
                    case DBEntityToDoList.ENUM_TODOSTATE.T0I:
                        dr[FieldName_STATETEXT] = DBEntityToDoList.ENUM_TODOSTATE.T0I_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0N:
                        dr[FieldName_STATETEXT] = DBEntityToDoList.ENUM_TODOSTATE.T0N_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0T:
                        dr[FieldName_STATETEXT] = DBEntityToDoList.ENUM_TODOSTATE.T0T_CHINESE;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0V:
                        dr[FieldName_STATETEXT] = DBEntityToDoList.ENUM_TODOSTATE.T0V_CHINESE;
                        break;
                }
            }
            dsTodoList.AcceptChanges();

            gridToDoList.DataSource = null;
            gridToDoList.DataSource = dsTodoList.Tables[0].DefaultView;


        }

        private void btnMarkCompleted_Click(object sender, EventArgs e)
        {
            int iRowHandle = gvToDoList.FocusedRowHandle;
            if (iRowHandle < 0)
            {
                MsgBox.MsgInfo("请选择要标记完成的代办事项。");
                return;
            }
            bool bFlag = MsgBox.MsgQue("确定要标记此待办事项为完成吗？");
            if (bFlag == true)
            {
                DataRow dr = gvToDoList.GetDataRow(iRowHandle);
                int todoListID = Convert.ToInt32(dr[DBEntityToDoList.FieldName.TODOLISTID]);
                string error = string.Empty;
                bFlag = BussinessProxyManager.Instance.MarkTodoListCompleted(todoListID, out error);
                if (bFlag == false)
                {
                    MsgBox.MsgInfo("标记待办事项完成失败\r\n" + error);
                    return;
                }
                else
                {
                    InitTodoList();
                }
            }
        }

        private void cbiStatus_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (gvToDoList.DataSource != null)
            {
                DataView dv = (DataView)gvToDoList.DataSource;
                string sSelectedText = cbiStatus.SelectedItem.ToString();
                string sSelectedValue = "";
                switch (sSelectedText)
                {
                    case DBEntityToDoList.ENUM_TODOSTATE.T0I_CHINESE:
                        sSelectedValue = DBEntityToDoList.ENUM_TODOSTATE.T0I;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0N_CHINESE:
                        sSelectedValue = DBEntityToDoList.ENUM_TODOSTATE.T0N;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0T_CHINESE:
                        sSelectedValue = DBEntityToDoList.ENUM_TODOSTATE.T0T;
                        break;
                    case DBEntityToDoList.ENUM_TODOSTATE.T0V_CHINESE:
                        sSelectedValue = DBEntityToDoList.ENUM_TODOSTATE.T0V;
                        break;
                    default:
                        sSelectedValue = "";
                        break;
                }
                if (sSelectedValue != "")
                    dv.RowFilter = string.Format("{0}='{1}'", DBEntityToDoList.FieldName.TODOSTATE, sSelectedValue);
                else
                    dv.RowFilter = "";

                gvToDoList_FocusedRowChanged(null, null);
            }
        }

        private void gvToDoList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            int iRowHandle = gvToDoList.FocusedRowHandle;
            if (iRowHandle < 0)
                return;

            DataRow dr = gvToDoList.GetDataRow(iRowHandle);

            string state = dr[DBEntityToDoList.FieldName.TODOSTATE].ToString();
            if (state.ToLower() == DBEntityToDoList.ENUM_TODOSTATE.T0V.ToLower())
            {
                btnMarkCompleted.Enabled = false;
            }
            else
            {
                btnMarkCompleted.Enabled = true;
            }
        }

        private void gvToDoList_DoubleClick(object sender, EventArgs e)
        {
            btnModify_Click(null, null);
        }

    }
}
